package com.springcloud.sc.ucpv5.client.util;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;

public class SystemUtil {
	// 设置环境变量
	public static void addLibraryDir(String libraryPath) throws IOException {
		try {
			Field field = ClassLoader.class.getDeclaredField("usr_paths");
			field.setAccessible(true);
			String[] paths = (String[]) field.get(null);
			for (int i = 0; i < paths.length; i++) {
				if (libraryPath.equals(paths[i])) {
					return;
				}
			}

			String[] tmp = new String[paths.length + 1];
			System.arraycopy(paths, 0, tmp, 0, paths.length);
			tmp[paths.length] = libraryPath;
			field.set(null, tmp);
		} catch (IllegalAccessException e) {
			throw new IOException("Failedto get permissions to set library path");
		} catch (NoSuchFieldException e) {
			throw new IOException("Failedto get field handle to set library path");
		}
	}

	/**
	 * 取得运行工程的绝对路径
	 * 
	 * @return
	 */
	public static String getProjectPath(String filePaht) {
		return getSystemPath() + filePaht;
	}

	private static String systemPath = null;

	public static String getSystemPath() {
		if (systemPath == null) {
			try {
				systemPath = new File(".").getCanonicalPath();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return systemPath + File.separator;
	}

}
